package sk.tuke.constraintbuilder.views.components;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import sk.tuke.constraintbuilder.model.Model;
import sk.tuke.constraintbuilder.model.Ruleset;

public class ApplyRulesetDialog extends TitleAreaDialog {

	ListViewer rulesetList;
	private Set<Ruleset> appliedRulesets = new HashSet<>();

	public ApplyRulesetDialog(Shell parentShell) {
		super(parentShell);
	}
	
	@Override
	public void create(){
//		Model.getModel().createModel();
		super.create();
		setTitle("Apply ruleset on project");
		setMessage("This is a TitleAreaDialog", IMessageProvider.INFORMATION);
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		parent.setLayout(layout);
		
		Composite listPanel = new Composite(parent, SWT.BORDER);
		listPanel.setLayout(new FillLayout());
		rulesetList = new ListViewer(listPanel, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		rulesetList.setContentProvider(ArrayContentProvider.getInstance());
		rulesetList.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				Ruleset ruleset = (Ruleset) element;
				return String.valueOf(ruleset.getName());
			};
		});
		rulesetList.setInput(Model.getModel().getRulesets());
		listPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
		
		Composite viewPanel = new Composite(parent, SWT.BORDER);
		viewPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
		return parent;
	}
	 
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button okButton = createButton(parent, OK, "Apply", true);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				okPressed();
		    }
		});
		Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
		        close();
		    }
		});
	}

	protected Button createOkButton(Composite parent, int id, String label, boolean defaultButton) {
		// increment the number of columns in the button bar
		((GridLayout) parent.getLayout()).numColumns++;
		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setFont(JFaceResources.getDialogFont());
		button.setData(new Integer(id));
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				okPressed();
		    }
		});
		if (defaultButton) {
			Shell shell = parent.getShell();
		    if (shell != null) {
		    	shell.setDefaultButton(button);
		    }
		}
		setButtonLayoutData(button);
		return button;
	}
		
	@Override
	protected void okPressed() {
		saveInput();
		super.okPressed();
	}
		  
	private void saveInput() {
		ISelection selection = rulesetList.getSelection();
      	if (selection != null && selection instanceof IStructuredSelection) {
      		Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
      		while(iterator.hasNext()){
      		Object obj = iterator.next();
          		if (obj != null) {
          			Ruleset ruleset = (Ruleset) obj;
          			appliedRulesets.add(ruleset);
          		}
      		}
      	}
	}
		  
	public Set<Ruleset> getAppliedRulesets(){
		return appliedRulesets;
	}
}
